package com.ziyun.pop.web.modules.freightages.controller;

import com.utility.JsonUtils;
import com.utility.New;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.freightages.FreightAreaEntity;
import com.ziyun.pop.modules.freightages.FreightTemplateEntity;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 运费模板表
 *
 * @author 
 * @email 
 * @date 2018-08-10 15:38:32
 */
@RestController
@RequestMapping("freightages/freighttemplate")
public class FreightTemplateController {

    @Value("${erpApi}")
    private String erpApi ;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("seller_id",UserUtils.getCurentUser().getSellerId());
        String path = "/freightages/freighttemplate/list";
        Map<String, Object> map = New.hashMap();
        map.put("params",params);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        return R.ok().put("page",resultMap.get("page")).put("userData",resultMap.get("userData"));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{templateId}")
    public R info(@PathVariable("templateId") Integer templateId){
        String path = "/freightages/freighttemplate/info";
        Map<String, Object> map = New.hashMap();
        map.put("templateId",templateId);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        return R.ok().put("freightTemplate", resultMap.get("freightTemplate")).put("freightAreaList",resultMap.get("freightAreaList"));
    }
//
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FreightTemplateEntity freightTemplate){
        freightTemplate.setSellerId(UserUtils.getCurentUser().getSellerId().intValue());
        freightTemplate.setCreateName(UserUtils.getCurentUser().getUsername());
        freightTemplate.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        List<FreightAreaEntity> freightAreaList = freightTemplate.getFreightAreaList();
//        for(int i = 0;i<freightAreaList.size();i++){
//            freightAreaList.get(i).setCreateName(UserUtils.getCurentUser().getUsername());
//        }
//        freightTemplateService.insert(freightTemplate);
        String path = "/freightages/freighttemplate/save";
        Map<String, Object> map = New.hashMap();
        map.put("freightTemplate",freightTemplate);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        return R.ok();
    }
//
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FreightTemplateEntity freightTemplate){
        freightTemplate.setUpdateName(UserUtils.getCurentUser().getUsername());
        freightTemplate.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        List<FreightAreaEntity> freightAreaList = freightTemplate.getFreightAreaList();
        for(int i = 0;i<freightAreaList.size();i++){
            freightAreaList.get(i).setUpdateName(UserUtils.getCurentUser().getUsername());
            freightAreaList.get(i).setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        }
        String path = "/freightages/freighttemplate/update";
        Map<String, Object> map = New.hashMap();
        map.put("freightTemplate",freightTemplate);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] templateIds){
        String path = "/freightages/freighttemplate/delete";
        Map<String, Object> map = New.hashMap();
        map.put("templateIds",templateIds);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/allList")
    public R allList(){
        String path = "/freightages/freighttemplate/allList";
        HashMap<String, Object> map = new HashMap<>();
        map.put("seller_id",UserUtils.getCurentUser().getSellerId());
        map.put("disabled",0);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
//        List<FreightTemplateEntity> freightTemplateList = freightTemplateService.selectByMap(map);
        return R.ok().put("freightTemplateList",resultMap.get("freightTemplateList"));
    }

    @RequestMapping("/newList")
    public R newList(@RequestBody Map<String, Object> map){
        String path = "/freightages/freighttemplate/newList";
        Map<String, Object> params = New.hashMap();
        params.put("current",map.get("current"));
        params.put("size",map.get("size"));
        params.put("total",map.get("total"));
        params.put("sellerId",UserUtils.getCurentUser().getSellerId());
        Map<String, Object> resultMap = remoteProcedureCall(params, path);
        return R.ok().put("allRegions",resultMap.get("allRegions")).put("page",resultMap.get("page"));
    }



    /**
     * 远程调用方法封装
     * @param params
     * @return
     */
    private Map<String, Object> remoteProcedureCall(@RequestParam Map<String, Object> params,String path) {
        String url = erpApi + path;
        Map<String, Object> paramHeaderMap = New.hashMap();
        Map<String, Object> paramBodyMap = New.hashMap();
        paramBodyMap.put("params",params);
        String response = HttpUtils.doPost(url, paramHeaderMap, JsonUtils.map2String(paramBodyMap));
        return com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
    }
}
